#region Imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using UserOrderType = PHSRAG.Insight21.Purchasing.UserOrderUtility.UserOrderType;
#endregion

namespace PHSRAG.Insight21.Purchasing
{
	#region UserOrderHelper Class
	/// <summary>
	/// This class contains static methods to help the user order object
	/// model to get vendor id and validate fund and account number.  
	/// </summary>
	public class UserOrderHelper
	{
		#region Constants
		private const int DefaultId						= 0;
		// The reconcile bit ensures that the returned datatable 
		// does not have an option "All".
		private const int ReconcileBit					= 0;
		// Corresponds to order type id of purchase order
		private const int DefaultOrderTypeID			= 1;
		

		//Stored Procedure Name
		private const string GetVendorIDSPName			= "GetVendorID";
		private const string GetOrderTypeListSPName		= "GetUserOrderTypeList";
		private const string GetOrderTypeSPName			= "GetOrderType";

		//Stored Procedure Parameter Name
		private const string VendorIDParamName			= "@vendorID";
		private const string VendorNameParamName		= "@vendorName";
		private const string VendorNumberParamName		= "@vendorNumber";
		
		// SP parameter name for getting order type list
		private const string IsReconcileParamName		= "@isReconcile";
		
		// SP parameter name for getting order type
		private const string OrderIDParamName			= "@orderID";
		private const string OrderTypeParamName			= "@orderType";

		#endregion

		#region Enumeration
		// Enumerates the arguments to the
		// stored procedure GetVendorID
		private enum GetVendorIDParamIndex
		{
			Id,
			Name,
			Number
		}

		// Enumeartes the stored proc agruments for 
		// getting the order type of given order id.
		private enum GetOrderTypeSPArgs
		{
			OrderID,
			OrderType
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Private Constructor
		/// </summary>
		private UserOrderHelper()
		{
		}
		#endregion

		#region Public methods

		#region GetVendorID
		
		/// <summary>
		/// Gets the vendor id from if the user has provided the vendor name or number.
		/// </summary>
		/// <param name="connectionString">open connection to database</param>
		public static int GetVendorId(SqlConnection connection, string vendorName, string vendorNumber)
		{
			int vendorID = 0;
			if(!vendorName.Equals(string.Empty) || !vendorNumber.Equals(string.Empty))
			{
				SqlParameter[] getVendorIdParams = Utility.DataAccess.BuildSqlParameterArray(
					VendorNameParamName, SqlDbType.VarChar, vendorName,
					VendorNumberParamName, SqlDbType.VarChar, vendorNumber);
			
				object vendorIDReturned = 
					Utility.DataAccess.ExecuteScalar(connection, GetVendorIDSPName, getVendorIdParams);
				
                vendorID = (vendorIDReturned == null) ? 0 : (int)vendorIDReturned;
			}
			return vendorID;
		}

		#endregion

		#region GetOrderTypeTable
		/// <summary>
		/// Gets the list of user order type. The reconcile bit is passed to the stored procedure as 0,
		/// which ensures that the returned datatable does not have an option "All".
		/// The data table returned is not cached because this property is called just once.
		/// </summary>
		public static DataTable GetOrderTypeTable()
		{
			return Utility.DataAccess.GetDataTable(Utility.DataAccess.GetConnectionString(), 
				GetOrderTypeListSPName, Utility.DataAccess.BuildSqlParameterArray(
				IsReconcileParamName, SqlDbType.Bit, ReconcileBit));			
		}
		#endregion

		#region GetOrderType
		/// <summary>
		/// Gets the order type for the given user order. It creates a
		/// sql parameter array of order id and ordertype(output parameter)
		/// and gets the order type for the order id from the database.
		/// </summary>
		/// <param name="orderID">id of the user order</param>
		/// <returns>order type for the given order id</returns>
		public static UserOrderType GetOrderType(int orderID)
		{
			SqlParameter[] userOrderSqlParams = Utility.DataAccess.BuildSqlParameterArray(
				OrderIDParamName, SqlDbType.Int, orderID);			

			object orderTypeReturned = 
				Utility.DataAccess.ExecuteScalar(Utility.DataAccess.GetConnectionString(), GetOrderTypeSPName,
				userOrderSqlParams);
			
			return (UserOrderType)((orderTypeReturned == null) ? DefaultOrderTypeID : (int)orderTypeReturned);			
		}
		#endregion

		#region UnCacheDependentReports

		/// <summary>
		/// Uncaches the data for the reports from the cache, whose
		/// underlying data is added, updated or deleted using the
		/// user order details screen. The dependent reports are:
		/// 1) UserOrderSummary
		/// 2) Reconcile Match/UnMatch
		/// </summary>
		/// <param name="cachePolicy">the prevalent cache policy</param>
		public static void UnCacheDependentReports(ICachePolicy cachePolicy)
		{
			new ReconcileMatchedOrders(cachePolicy).UnCache();
			new ReconcileUnMatchedOrders(cachePolicy).UnCache();
			new UserOrderSummaries(cachePolicy).UnCache();
		}
		#endregion

		#endregion
	}
	#endregion
}
